/**
 * 文章数据模型
 * 
 */
module.exports = class Article extends require('./model'){
    /**
     * 获取热门推荐文章
     * @param {int} num 条目数
     */
    static getHot(num){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT id,title,content,`time`,thumbnail FROM article WHERE hot = 1 LIMIT ?'
            // 父类继承的方法query
            this.query(sql,num).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`获取热门推荐文章失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 获取最新文章列表
     */
    static getList(start,size){
        let sql = 'SELECT id,title,content,`time`,thumbnail,author FROM article ORDER BY TIME DESC LIMIT ?,?';
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT count(id) AS count FROM article WHERE category_id = ? ORDER BY TIME DESC';
            this.query(sql,[start,size]).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`获取文章列表失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 获取指定目录下的文章列表文章总数
     * @param {int} id 类目编号
     */
    static getCategoryCount(id){
        // let sql = 'SELECT id,title,content,`time`,thumbnail,author FROM article WHERE category_id = ? ORDER BY TIME DESC';
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT count(id) AS count FROM article WHERE category_id = ? ORDER BY TIME DESC';
            this.query(sql,id).then(res =>{
                resolve(res[0].count);
            }).catch(err =>{
                console.log(`获取指定列表文章总数失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 获取指定目录下的文章列表
     * @param {int} id 类目编号
     */
    static getListCategoryId(id,start,size){
        // let sql = 'SELECT id,title,content,`time`,thumbnail,author FROM article WHERE category_id = ? ORDER BY TIME DESC';
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT id,title,content,`time`,thumbnail,author FROM article WHERE category_id = ? ORDER BY TIME DESC LIMIT ?,?';
            this.query(sql,[id,start,size]).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`获取指定目录下的文章列表失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 获取指定关键词的文章列表
     * @param {int} id 类目编号
     */
    static getListBykeywrod(keyword){
        let sql = "SELECT id,title,content,`time`,thumbnail,author FROM article WHERE title LIKE ? ORDER BY TIME DESC";
        return this.getPromise(sql,'获取指定关键词的文章列表失败',`%${keyword}%`);
    }
    /**
     * 获取指定文章的详情
     * @param {int} id 文章编号
     */
    static getArticleById(id){
        return new Promise((resolve,reject) =>{
            let sql = "SELECT a.id,a.title,a.author,a.content,a.`time`,a.hits,a.category_id,a.hot,a.`thumbnail`,c.name FROM article a,category c WHERE a.id= ? AND a.`category_id` = c.id";
            // 父类继承的方法query
            this.query(sql,id).then(res =>{
                resolve(res[0]);
            }).catch(err =>{
                console.log(`获取指定文章的详情失败:${err.message}`);
                reject(err);
            });
        });
    }

    /**
     * 获取上一篇文章
     * @param {int} id 当前的文章编号
    */
    static getPrevArticle(id){
        return new Promise((resolve,reject) =>{
            // let sql = "SELECT id,title FROM `article` WHERE id < ? ORDER BY id DESC LIMIT 1";
            let sql = " select * from article where category_id in (select category_id from article where id = ? ORDER BY TIME DESC) and id > ? LIMIT 1"
            // 父类继承的方法query
            this.query(sql,[id,id]).then(res =>{
                resolve(res[0]);
            }).catch(err =>{
                console.log(`获取上一篇文章失败:${err.message}`);
                reject(err);
            });
        });
    }

    /**
     * 获取下一篇文章
     * @param {int} id 当前的文章编号
    */
    static getNextArticle(id){
        return new Promise((resolve,reject) =>{
            // let sql = "SELECT id,title FROM `article` WHERE id > ? ORDER BY id ASC LIMIT 1";
            let sql = " select * from article where category_id in (select category_id from article where id = ? ORDER BY TIME DESC) and id < ? LIMIT 1"
            // 父类继承的方法query
            this.query(sql,[id,id]).then(res =>{
                resolve(res[0]);
            }).catch(err =>{
                console.log(`获取下一篇文章失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     *  前台获取总博文数
    */
    static getCount(){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT count(id) AS count FROM `article`';
            // 父类继承的方法query
            this.query(sql).then(res =>{
                resolve(res[0].count);
            }).catch(err =>{
                console.log(`数据库获取总博文数失败:${err.message}`);
                reject(err);
            });
        });
    }

    /**
     * 前台获取指定页的文章列表
     * @param {int} start 第几页
     * @param {int} size 显示个数
    */
   static getPage(start,size){
        return new Promise((resolve,reject) =>{
            let sql = "SELECT id,title,content,`time`,thumbnail,author,hot FROM article WHERE 1=1 ORDER BY id DESC LIMIT ?,?";
            // 父类继承的方法query
            this.query(sql,[start,size]).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`数据库获取指定页的文章列表失败:${err.message}`);
                reject(err);
            });
        });
    }

    /**
     *  后台获取总博文数
     */
    static getCountEnd(category_id,hot){
        return new Promise((resolve,reject) =>{
            let sql = 'SELECT COUNT(1) AS count FROM `article` WHERE 1=1';
            sql += category_id != -1 && category_id ? ` AND category_id = ${category_id}`:'';
            sql += hot != -1 && hot ? ` AND hot = ${hot}`:"";

            // 父类继承的方法query
            this.query(sql).then(res =>{
                resolve(res[0].count);
            }).catch(err =>{
                console.log(`数据库后台获取总博文数失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 后台获取指定页的文章列表
     * @param {int} start 第几页
     * @param {int} size 显示个数
    */
   static getPageEnd(start,size,category_id,hot){
        return new Promise((resolve,reject) =>{
            let sql = "SELECT id,title,content,`time`,thumbnail,author,hot FROM article WHERE 1=1";
            sql += category_id !=-1 && category_id ? ` AND category_id = ${category_id}`:'';
            sql += hot != -1 && hot ? ` AND hot = ${hot}`:"";
            sql += ' ORDER BY id DESC LIMIT ?,?';
            // 父类继承的方法query
            this.query(sql,[start,size]).then(res =>{
                resolve(res);
            }).catch(err =>{
                console.log(`数据库后台获取指定页的文章列表失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 设置热门
     * @param {int} id 文章编号
     * @param {int} hot 热门状态
    */
   static setHot(id,hot){
        return new Promise((resolve,reject) =>{
            let sql = "UPDATE article SET hot = ? WHERE id = ?";
            // 父类继承的方法query
            this.query(sql,[hot,id]).then(res =>{
                resolve(res.affectedRows);
            }).catch(err =>{
                console.log(`数据库设置热门失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 添加文章
     * @param {Object} article 添加的对象
    */
   static add(article){
        return new Promise((resolve,reject) =>{
            let sql = "INSERT INTO `article` SET ?";
            // 父类继承的方法query
            this.query(sql,article).then(res =>{
                resolve(res.insertId);
            }).catch(err =>{
                console.log(`数据库添加文章失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 删除文章
     * * @param {int} id 文章编号
    */
    static del(id){
        return new Promise((resolve,reject) =>{
            let sql = "DELETE FROM article WHERE id = ?";
            // 父类继承的方法query
            this.query(sql,id).then(res =>{
                resolve(res.affectedRows);
            }).catch(err =>{
                console.log(`数据库删除文章失败:${err.message}`);
                reject(err);
            });
        });
    }
    /**
     * 编辑文章
     * * @param {Object} article 文章对象
    */
    static edit(article){
        return new Promise((resolve,reject) =>{
            let sql = "UPDATE `article` SET title= ?,content = ?,hot = ?,category_id = ?,thumbnail = ? WHERE id = ?";
            // 父类继承的方法query
            this.query(sql,[article.title,article.content,article.hot,article.category_id,article.thumbnail,article.id]).then(res =>{
                resolve(res.affectedRows);
            }).catch(err =>{
                console.log(`数据库编辑文章失败:${err.message}`);
                reject(err);
            });
        });
    }
}